package com.example.myweather

import android.os.Bundle

import com.example.myweather.bean.WeatherApi
import com.example.myweather.tools.HttpUtils
import com.example.myweather.tools.MainApplication
import com.google.android.material.floatingactionbutton.FloatingActionButton
import com.google.android.material.snackbar.Snackbar
import com.google.android.material.tabs.TabLayout

import androidx.viewpager.widget.ViewPager
import androidx.appcompat.app.AppCompatActivity

import android.util.Log
import android.view.Menu
import android.view.MenuItem
import android.view.View

import com.example.myweather.ui.main.SectionsPagerAdapter
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.umeng.message.PushAgent
//import com.google.gson.Gson;

import org.json.JSONArray
import org.json.JSONObject

import java.io.IOException
import java.lang.reflect.Type
import java.util.ArrayList

import okhttp3.Call
import okhttp3.Response

class MainActivity : AppCompatActivity() {

    internal var url = "https://api.caiyunapp.com/v2.5/y1S3sjxaYCYMos7N/121.6544,25.1552/weather.json"


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        PushAgent.getInstance(MainApplication.context).onAppStart()
        setContentView(R.layout.activity_main)
        val sectionsPagerAdapter = SectionsPagerAdapter(this, supportFragmentManager)
        val viewPager = findViewById<ViewPager>(R.id.view_pager)
        viewPager.adapter = sectionsPagerAdapter
        val tabs = findViewById<TabLayout>(R.id.tabs)
        tabs.setupWithViewPager(viewPager)
        val fab = findViewById<FloatingActionButton>(R.id.fab)

        fab.setOnClickListener { view ->
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show()
            sendRequestWithOkHttp()
        }

    }

    private fun sendRequestWithOkHttp() {
        Thread(Runnable {
            //在子线程中执行Http请求，并将最终的请求结果回调到okhttp3.Callback中
            HttpUtils.sendOkHttpRequest(url, object : okhttp3.Callback {
                @Throws(IOException::class)
                override fun onResponse(call: Call, response: Response) {
                    //得到服务器返回的具体内容
                    val responseData = response.body!!.string()
                    parseJSONWithGSON(responseData)
                    //显示UI界面，调用的showResponse方法
                    showResponse(responseData)
                }

                override fun onFailure(call: Call, e: IOException) {
                    //在这里进行异常情况处理
                }
            })
        }).start()
    }

    private fun parseJSONWithGSON(jsonData: String) {
        //使用轻量级的Gson解析得到的json
        val gson = Gson()

        //       List<WeatherApi> weatherApiList = gson.fromJson(jsonData, new TypeToken<List<WeatherApi>>() {}.getType());
        //        for (WeatherApi weatherApi : weatherApiList) {
        //            //控制台输出结果，便于查看
        //              Log.d("MainActivity", "other" + weatherApi.getStatus());
        ////            Log.d("MainActivity", "u_Name" + app.getU_Name());
        ////            Log.d("MainActivity", "u_id" + app.getU_id());
        ////            Log.d("MainActivity", "u_passWord" + app.getU_passWord());
        ////            Log.d("MainActivity", "u_token" + app.getU_token());
        //        }

        val weatherApi = gson.fromJson(jsonData, WeatherApi::class.java)
        Log.d("MainActivity", "status:" + weatherApi.status)
        Log.d("MainActivity", "status:" + weatherApi.result.daily.skycon_20h_32h[3].value)

    }

    private fun showResponse(response: String) {
        //在子线程中更新UI
        runOnUiThread {
            // 在这里进行UI操作，将结果显示到界面上
            //                responseText.setText(response);
            //Log.d("MainActivity", response);
        }
    }
}